package com.servlet;

import com.beans.AdminInfo;
import com.dao.AdminDao;
import com.util.Des;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private AdminDao adminDao = new AdminDao();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String adminName = request.getParameter("adminName");
        String password = request.getParameter("password");

        AdminInfo admin = adminDao.login(adminName,Des.encStr(password));

        System.out.println(admin);
        String result = "1";

        if (admin==null){
            result = "0";
        }else if (admin.getState().equals("2")){
            result = "2";
        }
        if ("1".equals(result)){
            request.getSession().setAttribute("session_admin",admin);
        }
        response.getWriter().print(result);
    }

}

